import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { Router } from '@angular/router';

@Component({
    templateUrl: './index.html',
    styleUrls: ['./index.scss']
})
export class CPDelComponent implements OnInit {
    AgencyList: Array<any>;
    GuideList: Array<any> = [];
    validateForm;

    page = {
        pageNo: 1,
        name: '',
        travelAgencyId: '',
        beginTime: '',
        endTime: '',
    };

    constructor(
        private apiService: ApiService,
        private message: MessageService,
        private router: Router,
        private fb: FormBuilder,
        private nzmessage: NzMessageService
    ) {
    }
    ngOnInit() {
        this.message.setBreadcrumb({
            breadcrumbs: [
                { name: '创建产品' }
            ]
        });
        this.validateForm = this.fb.group({
			providerType: ['', [Validators.required]],
			displayPriority: [null, [Validators.required]],
            agencyId: [''],
            guideId: [''],
            title: ['', [Validators.required]],
            url: [''],
            image: ['', [Validators.required]],
            status: ['']
        });
        this.loadList('旅游社列表(用于选择框)', {});
        this.loadList('导游列表', { ...this.page });
    }
    loadList(url, data) {
        this.apiService.post(url, data).then((res: any) => {
            switch (url) {
                case '旅游社列表(用于选择框)':
                    this.AgencyList = res.data.list;
                    break;
                case '导游列表':
                    this.GuideList = res.data.list;
                    break;

                default:
                    break;
            }
        });

    }
    selectedRow(item) {
        this.validateForm.patchValue({ guideId: item.id });
    }

    submit() {
        console.log(this.validateForm.getRawValue())
        let data = this.validateForm.getRawValue();

        console.log(data)
        this.apiService.post('旅游产品创建', data).then((res: any) => {
            this.router.navigate(['basicSetting/cp']);
        }).catch((res) => {
            this.nzmessage.error(res.data.message);
        });;
    }
}
